{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2c7e2eda-4f81-40b2-bc5d-c6424d3074a5",
   "metadata": {},
   "source": [
    "# 字典\n",
    "\n",
    "# 创建字典的用法\n",
    "- 字符为大括号\n",
    "- 数据为键值对形式出现\n",
    "- "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56cfe6ed-a40c-4d8b-860a-0f13aaa22258",
   "metadata": {},
   "source": [
    "从上面的操作中可以看出，字典是支持赋值操作的\n",
    "注意：一般冒号前面的为key,简称k; 冒号后面的为value, 简称 v"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ae2a1b8b-5fb5-4a1a-976b-06b497e2097f",
   "metadata": {},
   "source": [
    "### 增加操作\n",
    "写法：<font style=\"color=orange\" 字典序列[key] = 值>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebecaeff-c45a-4465-83eb-4c692c1e174f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# coding:UTF-8\n",
    "dict1 = {'name': '张三', 'age': 22, 'gender': '男' }"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5632ea01-a400-4c3c-9a75-a2052b1e6f7e",
   "metadata": {},
   "source": [
    "## 修改操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b9b7d35-c73c-4b99-8e44-01da6c9dfe98",
   "metadata": {},
   "source": [
    "## 查操作\n",
    "### key值查找"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c3b674a2-62c0-4401-9513-225315892374",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "dict1 = {'name': '张三', 'age': 22, 'gender': '男' }\n",
    "\n",
    "print(dict1['name'])     # 该name对应的value值\n",
    "\n",
    "# print(dict1['id'])    # KerError: 'id' 如果没有找到，则会报错"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd308f30-88d3-4dd7-8e89-efab9e0ef1be",
   "metadata": {},
   "source": [
    "get() 取得，获得\n",
    "字典序列.get(key,默认值)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2937932c-fa07-4b4e-ba38-661819de5142",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三\n",
      "111\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "dict1 = {'name': '张三', 'age': 22, 'gender': '男' }\n",
    "\n",
    "print(dict1.get('name'))    # 获取name对应的value内容\n",
    "print(dict1.get('id', 111))\n",
    "print(dict1.get('id'))   # None 由于没有id这个键值对，所以它会返回空值n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5197ca9-962d-47d4-8c6a-113508345633",
   "metadata": {},
   "source": [
    "keys() 内置函数会输出字典当中的所有的key的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9f92287f-6006-4345-8f3d-9c4754c2beb2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys(['name', 'age', 'gender'])\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "dict1 = {'name': '张三', 'age': 22, 'gender': '男' }\n",
    "\n",
    "print(dict1.keys())    # dict_keys(['name', 'age', 'gender'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52db432a-1ac9-4fe9-ac94-0d4299cc1ae9",
   "metadata": {},
   "source": [
    "items()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "11657991-b303-4be1-8b7c-9b6f37f9f93f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_items([('name', '张三'), ('age', 22), ('gender', '男')])\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "dict1 = {'name': '张三', 'age': 22, 'gender': '男' }\n",
    "print(dict1.items())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55cceec5-97f7-459f-a0a4-ba99a455ed0f",
   "metadata": {},
   "source": [
    "## 字典的循环应用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4d5f30b4-dfe1-470a-86cd-4c403cbb4e81",
   "metadata": {},
   "source": [
    "#### 1.遍历激活字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9188734a-ff10-48e4-81fb-d1723fcb1d27",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name\n",
      "age\n",
      "gender\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "dict1 = {'name':'张三','age': 22,'gender': '男'}\n",
    "\n",
    "for key in dict1.keys():\n",
    "    print(key)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "870066d6-3165-4a9b-8e9d-8e99858cc3a3",
   "metadata": {},
   "source": [
    "#### 2.遍历字典的value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "addf625b-af5f-468a-b6cc-00bac34813b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三\n",
      "22\n",
      "男\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "dict1 = {'name':'张三','age': 22,'gender': '男'}\n",
    "\n",
    "for value in dict1.values():\n",
    "    print(value)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "faff3729-387d-4b6c-a7f8-02ac6f220814",
   "metadata": {},
   "source": [
    "#### 3.遍历字典元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "762e0fee-db27-4c12-ac3f-ba8eab2aacdd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('name', '张三')\n",
      "('age', 22)\n",
      "('gender', '男')\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "dict1 = {'name':'张三','age': 22,'gender': '男'}\n",
    "\n",
    "for item in dict1.items():\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dcd687a5-3b40-4539-8af3-b8cc469a7eef",
   "metadata": {},
   "source": [
    "#### 4.遍历字典的键值对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "e5662293-0c07-4b44-b65e-1a7abd75efb4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name = 张三\n",
      "age = 22\n",
      "gender = 男\n"
     ]
    }
   ],
   "source": [
    "# coding:UTF-8\n",
    "\n",
    "dict1 = {'name':'张三','age': 22,'gender': '男'}\n",
    "\n",
    "for key,value in dict1.items():\n",
    "    print(f'{key} = {value}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "833117a3-29ef-4d7d-b146-6c3e9e8aa597",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
